#ifndef __FIBER_H__
#define __FIBER_H__

typedef struct fiber_s *fiber_t;

struct context_s
{
	 uintptr_t   stk_ptr;
	 uintptr_t   stk_top;

	 uintptr_t  pc;
};

struct sched_node_s
{
	 struct sched_node_s *prev, *next;
};

struct fiber_s
{
	 struct context_s    ctx;
	 struct sched_node_s sched_node;
	 int status;
	 int uevent_touched;
	 struct uthread_priv_s *upriv;
};

struct uthread_priv_s
{
	 fiber_t fiber_cur;
	 struct sched_node_s sched_node;
	 struct sched_node_s low_sched_node;
	 
	 int in_sched;
	 int msg_incoming;

	 uint32_t  io_count;
	 uint32_t  io_buf_cap;
	 uint32_t *io_buf;
};

extern struct uthread_priv_s *upriv;

typedef void (*fiber_entry_f) (void *args);

int     fiber_init(fiber_entry_f entry, void *arg);
fiber_t fiber_create(fiber_entry_f entry, void *arg);
int     fiber_thread_create(fiber_entry_f entry, void *arg);

fiber_t fiber_self(void);

void fiber_process_io_buffer(void);
void fiber_yield_io(void);
void fiber_yield_low(void);
void fiber_yield(void);

void fiber_wait_pretend(void);
void fiber_wait_try(void);
void fiber_notify(fiber_t f);

#endif
